package com.yoda.mall.mysql.api;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.CouponApi;
import com.yoda.mall.model.entity.sms.Coupon;
import com.yoda.mall.mysql.mapper.CouponMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class CouponApiImpl implements CouponApi {
    @Autowired
    private CouponMapper couponMapper;

    @Override
    public void createCoupon(Coupon coupon) {
        couponMapper.insert(coupon);
    }

    @Override
    public void deleteCouponById(Long id) {
        couponMapper.deleteById(id);
    }

    @Override
    public Page<Coupon> getCouponListPage(String name, Integer pageNum, Integer pageSize, Integer type) {
        Page<Coupon> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Coupon> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotEmpty(name), Coupon::getName, name);
        lqw.eq(ObjectUtil.isNotNull(type), Coupon::getType, type);
        couponMapper.selectPage(page, lqw);
        return page;
    }

    @Override
    public void updateCoupon(Coupon coupon) {
        couponMapper.updateById(coupon);
    }

    @Override
    public Coupon getCouponById(Long id) {
        return couponMapper.selectById(id);
    }
}
